package shuai.controller;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

import shuai.action.BaseAction;
import shuai.model.User;

public class UserController extends BaseAction {
	Session session=null;
	Configuration configuration=new AnnotationConfiguration().configure();
	SessionFactory factory=configuration.buildSessionFactory();

	public User userLogin(String username,String userpwd){
		session=factory.openSession();
		String hql=" from 用户  where username=:name and userpwd=:pwd";
		Query query=session.createQuery(hql);
		query.setString("name", username);
		query.setString("pwd", userpwd);
		List<User> users=query.list();
		
		session.close();
		if(users!=null){
			return users.get(0);
		}else{
			return null;
		}
	}
	
	public int userRegist(User user){
		session=factory.openSession();
		Transaction transaction=session.beginTransaction();
		int flag=0;
		try {
			session.save(user);
			transaction.commit();
			flag=1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		session.close();
		return flag;		
	}
}
